package com.ruoyi.app.service.impl

import com.alipay.easysdk.factory.Factory
import com.alipay.easysdk.kernel.util.ResponseChecker
import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest
import com.github.binarywang.wxpay.service.WxPayService
import com.ruoyi.app.common.*
import com.ruoyi.app.service.PayApiService
import com.ruoyi.back.domain.dto.UserMemberOrderDto
import com.ruoyi.back.enums.PayTypeEnum
import com.ruoyi.back.service.MemberService
import com.ruoyi.back.service.UserMemberOrderService
import com.ruoyi.back.service.UserMemberService
import com.ruoyi.common.utils.ip.IpUtils
import com.ruoyi.config.PayNotifyParam
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.stereotype.Service
import javax.servlet.http.HttpServletRequest

@Service
@EnableConfigurationProperties(PayNotifyParam::class)
class PayApiServiceImpl : PayApiService {

    private final val log = LoggerFactory.getLogger(PayApiServiceImpl::class.java)

    @Autowired
    private lateinit var wxService: WxPayService

    @Autowired
    private lateinit var userMemberOrderService: UserMemberOrderService

    @Autowired
    private lateinit var memberService: MemberService

    @Autowired
    private lateinit var userMemberService: UserMemberService

    @Autowired
    private lateinit var payNotifyParam: PayNotifyParam

    override fun <T> createWxOrder(userMemberOrderDto: UserMemberOrderDto): Data<T> =
        Ok("创建微信预支付成功",
            userMemberOrderService.createUserMemberOrder(userMemberOrderDto.apply {
                payType = PayTypeEnum.WEI_XIN.code
            })
                .let {
                    this.wxService.createOrder(WxPayUnifiedOrderRequest().apply {
                        body = memberService.getNameByMemberId(it.memberId!!)
                        totalFee = it.payAmount?.fen()
                        notifyUrl = "http://face.hfyanlv.com/stage-api/api/pay/wxNotify"
                        outTradeNo = it.orderNum
                        spbillCreateIp = IpUtils.getHostIp()
                        tradeType = "APP"
                    })
                }
        )

    override fun createAliOrder(userMemberOrderDto: UserMemberOrderDto): Data<String> =
        userMemberOrderService.createUserMemberOrder(userMemberOrderDto.apply { payType = PayTypeEnum.ALI_PAY.code })
            .let {
                Factory.Payment.App().pay(
                    memberService.getNameByMemberId(it.memberId!!),
                    it.orderNum,
                    memberService.getById(userMemberOrderDto.memberId)?.price.toString()
                )
                    .takeIf { alipayTradeAppPayResponse -> ResponseChecker.success(alipayTradeAppPayResponse) }
                    ?.run {
                        Ok("获取预支付参数成功", body)
                    }
                    ?: kotlin.run {
                        log.error(
                            "支付宝App支付(系统)失败",
                        )
                        Fail("支付失败")
                    }
            }

    override fun wxNotify(xmlData: String): String =
        wxService.parseOrderNotifyResult(xmlData).run {

            log.info("--------------进入到了回调------------------:$xmlData")
            userMemberOrderService.updateMemberPayStatus(outTradeNo, PayTypeEnum.WEI_XIN)
            WxPayNotifyResponse.success("成功")
        }

    override fun aliNotify(request: HttpServletRequest): String {
//        Factory.Payment.Common().takeIf { it.verifyNotify(request.params())}
//            ?.run {
//
//            }
//            ?: kotlin.run {
//                log.error("验证签名失败")
//                "fail"
//            }

        log.info("--------------进入到了回调------------------")
        userMemberOrderService.getUserMemberOrderByOrderNum(request.getParameter("out_trade_no"))
            ?.run {
                userMemberOrderService.updateMemberPayStatus(orderNum!!, PayTypeEnum.ALI_PAY)
            }
        return "success"
    }

}